package com.aiju.cash.ui.signbank.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.common.Verify.EmptyVerify;
import com.aiju.common.Verify.VerifyManger;
import com.aiju.forexpand.datepicker.DatePicker;
import com.aiju.utils.DateUtil;
import com.aiju.utils.L;
import com.aiju.widgit.layout.SettingItemTextView;

/**
 * @author: 流冥
 * @date: 2017-07-13
 * @Description: 设置营业时间
 */
public class SettingBusinessTimeActivity extends BaseTitleActivity {
    private SettingItemTextView start;
    private SettingItemTextView end;
    private String s;
    private String e;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_business_time);
        initView();
    }

    void initView() {
        mAiJuTitleBarView.setCenterTitle("设置营业时间");
        mAiJuTitleBarView.setRightTitle("提交");
        start = (SettingItemTextView) findViewById(R.id.start_time);
        end = (SettingItemTextView) findViewById(R.id.end_time);
        mAiJuTitleBarView.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInfo()) {
                    L.i("info", s + "   " + e);
                    Intent intent = new Intent();
                    intent.putExtra("time", s + "-" + e);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEffectTime(start.getRightView(), "设置营业开始时间");
            }
        });
        end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showExpireTime(end.getRightView(), "设置营业结束时间");
            }
        });
    }


    /**
     * 开始时间
     */
    private void showEffectTime(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectTimeDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatHM));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate( new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                s = date;
            }
        });
    }

    /**
     * 结束时间
     */
    private void showExpireTime(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectTimeDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatHM));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                e = date;
            }
        });
    }

    boolean checkInfo() {
        return new VerifyManger()
                .addVerify(new EmptyVerify(s, "请设置营业开始时间"))
                .addVerify(new EmptyVerify(e, "请设置营业结束时间")).execute();
    }
}
